<?php
	session_start();
?>
<?php
	include_once "connection.php";
?>
<?php
	$user_id = $_SESSION["user_id"];
?>
<?php
	$post_title = $_POST["post_title"];
	$special_image = $_POST["special_image"];
	$post_content = $_POST["post_context"];
?>
<?php
	/* 该页面用于存储文章 */
	$sql = "insert into posts(post_title, special_image, post_content, user_id, post_date) values(?, ?, ?, ?, ?)";
	$message = array();
	try{
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$dbh->beginTransaction();

		$result = $dbh->prepare($sql);      /* 使用PDO::prepare()防止SQL注入 */
		$result->execute(array($post_title, $special_image, $post_content, $user_id, date('Y-m-d H:i:s')));
		$message[0] = "文章发表成功";
		$post_id = $dbh->lastInsertId();
		$message[1] = "<a href=\"post_read.php?post_id=$post_id\" target=\"_top\">查看文章</a>";

		$dbh->commit();
	}
	catch(Exception $e){
		$dbh->rollBack();
		$message[0] = "文章发表失败";
		$message[1] = $e->getMessage();
	}
?>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
		<title>发表文章</title>
        <style type="text/css">
            body{
                background: #fff;
            }
            .info{
                height: 18px;
                width: 300px;
                margin: 0 auto;
                margin-top: 6px;
                color: #444;
                font-size: 18px;
                text-align: center;
            }
        </style>
	</head>
	<body>
        <div style=" width:300px; height:100px;">
        </div>
<?php
	foreach($message as $info) {
		echo "<div class=\"info\">$info</div>";
}
?>
	</body>
</html>